package com.arcgis.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.arcgis.app.R;
import com.arcgis.app.utils.StringUtils;
import com.bumptech.glide.Glide;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class SelectPictureAdapter extends RecyclerView.Adapter<SelectPictureAdapter.MyTVHolder> {

    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private final ArrayList<String> mData;
    private OnItemClick onitemClick;
    private OnCloseImageClick onCloseImageClick;
    private int maxCount = 5;
    private  boolean isShowAdd;

    public interface OnItemClick {
        void onItemClick(int position);
    }

    public interface  OnCloseImageClick{
        void onCloseImageClick(int position);
    }

    //定义设置点击事件监听的方法
    public void setonItemClickListener(OnItemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    public void setOnCloseImageClick(OnCloseImageClick onCloseImageClick){
        this.onCloseImageClick = onCloseImageClick;
    }

    public SelectPictureAdapter(Context context, ArrayList<String> data) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        isShowAdd = true;
        mData = data;
    }

    @Override
    public SelectPictureAdapter.MyTVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectPictureAdapter.MyTVHolder(mLayoutInflater.inflate(R.layout.upload_image_cell, parent, false));
    }

    @Override
    public void onBindViewHolder(final SelectPictureAdapter.MyTVHolder holder, final int pos) {
//        holder.mTextView.setText(mData.get(pos));
        if (mData.size() > 0 && pos < mData.size()){
            Log.i("SelectPicture",mData.get(pos));
            if (StringUtils.isNetworkURL(mData.get(pos))){
//                Glide.with(mContext).load(mData.get(pos)).into(holder.imageButton);
                Glide.with(mContext).load(mData.get(pos)).centerCrop().into(holder.imageButton);
            }else {
                try {
                    FileInputStream fis = new FileInputStream(mData.get(pos));
                    Bitmap bitmap = BitmapFactory.decodeStream(fis);
                    holder.imageButton.setImageBitmap(bitmap);
                    holder.removeBtn.setVisibility(View.VISIBLE);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

        }
        else{
            holder.imageButton.setImageDrawable(mContext.getDrawable(R.drawable.add));
            holder.removeBtn.setVisibility(View.GONE);
        }

        if (onCloseImageClick != null){
            holder.removeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onCloseImageClick.onCloseImageClick(pos);
                }
            });
        }

        if (onitemClick != null) {
            holder.imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //在TextView的地方进行监听点击事件，并且实现接口
                    if (mData.size() == 0 || ((mData.size() + 1) <= maxCount && pos == (mData.size()))){
                        onitemClick.onItemClick(pos);
                    }
                }
            });
        }
    }


    @Override
    public int getItemCount() {
            if (mData == null || mData.size() == 0){
                return  1;
            }
            if (mData.size() < maxCount ){
                return mData.size() + 1;
            }
            if (mData.size() == maxCount){
                return  maxCount;
            }
            return 1;
    }

    class MyTVHolder extends RecyclerView.ViewHolder {
        ImageButton imageButton;
        ImageButton removeBtn;

        MyTVHolder(View itemView) {
            super(itemView);
            imageButton = (ImageButton)itemView.findViewById(R.id.btn_message_attach_info);
            removeBtn = (ImageButton)itemView.findViewById(R.id.btn_message_attach_remove);
        }
    }
}
